package com.hepinghospital.user.ui.dialog;

import android.app.Dialog;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hepinghospital.user.R;
import com.hepinghospital.user.adapter.OffLineAdapter;
import com.hepinghospital.user.base.BaseDialogFragment;
import com.hepinghospital.user.util.ToastUtil;
import com.rest.business.RestProxy;
import com.rest.observer.BaseObserver;
import com.rest.response.ScheduleListResponse;

import java.util.ArrayList;
import java.util.List;

import jz.joyoung.robot.util.DpUtil;

/**
 * @author Administrator
 * @date 2017/11/23
 */

public class OffLineDialogFragment extends BaseDialogFragment {


    private String schTime;
    private String scheduleId;
    private String scheduleTime;
    private int lastPosition = -1;

    public static OffLineDialogFragment newInstance(String docId, String deptId, String schDate, String timeInterval, String scheduleTime) {
        Bundle args = new Bundle();
        args.putString("docId", docId);
        args.putString("deptId", deptId);
        args.putString("schDate", schDate);
        args.putString("timeInterval", timeInterval);
        args.putString("scheduleTime", scheduleTime);
        OffLineDialogFragment fragment = new OffLineDialogFragment();
        fragment.setArguments(args);
        return fragment;
    }

    private Dialog mDialog;
    private OnItemClickListener mListener;
    private RecyclerView recyclerView;
    private OffLineAdapter adapter;
    private String docId, deptId, schDate, timeInterval, price, number;
    private TextView tvConfirm, tvScheduleTime;

    private List<ScheduleListResponse.Schedule> list = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_offline, container);
        recyclerView = view.findViewById(R.id.recyclerView);
        tvConfirm = view.findViewById(R.id.tv_confirm);
        tvScheduleTime = view.findViewById(R.id.tv_schedule_time);
        GridLayoutManager manager = new GridLayoutManager(getActivity(), 1);
        recyclerView.setLayoutManager(manager);
        adapter = new OffLineAdapter(getActivity(), list);
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
                super.onDraw(c, parent, state);
            }

            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.right = DpUtil.dip2px(getActivity(), 10);
                outRect.left = DpUtil.dip2px(getActivity(), 10);
                outRect.bottom = DpUtil.dip2px(getActivity(), 10);
            }
        });
        if (getArguments() != null) {
            docId = getArguments().getString("docId");
            deptId = getArguments().getString("deptId");
            schDate = getArguments().getString("schDate");
            timeInterval = getArguments().getString("timeInterval");
            scheduleTime = getArguments().getString("scheduleTime");
            getScheduleTimeList();
        }


        tvScheduleTime.setText(scheduleTime);
        tvConfirm.setOnClickListener(v -> {
            if (mListener != null) {
                mListener.onConfirm(schTime, scheduleTime, scheduleId, price, number);
            }
            dismiss();
        });
        adapter.setOnItemClickLitener((view1, position) -> {
            adapter.setPosition(position);
            adapter.notifyDataSetChanged();
            schTime = list.get(position).schTime;
            scheduleId = list.get(position).scheduleId;
            number = (position + 1) + "";
            price = list.get(position).price;
            Log.d("price", price);
        });
        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        mDialog = super.onCreateDialog(savedInstanceState);
        mDialog.setCancelable(false);
        Window window = mDialog.getWindow();
        if (window != null) {
            window.setGravity(Gravity.BOTTOM);
        }
        return mDialog;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0x00000000));
        getDialog().getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
    }

    public OnItemClickListener getmListener() {
        return mListener;
    }

    public void setmListener(OnItemClickListener mListener) {
        this.mListener = mListener;
    }


    public interface OnItemClickListener {

        void onConfirm(String time, String scheduleTime, String id, String price, String number);
    }

    public boolean isShowing() {
        return mDialog != null && mDialog.isShowing();
    }


    class MyAdapter extends RecyclerView.Adapter<MyAdapter.Holder> {

        @Override
        public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            Holder myHolder;
            myHolder = new Holder(LayoutInflater.from(getActivity()).inflate(R.layout.item_offline_time, parent, false));
            return myHolder;
        }

        @Override
        public void onBindViewHolder(Holder holder, final int position) {
            holder.itemView.setOnClickListener(v -> {
                if (list.get(position).flag == 1) {
                    schTime = list.get(position).schTime;
                    scheduleId = list.get(position).scheduleId;
                    price = list.get(position).price;
                    holder.tv_dateTime.setTextColor(ContextCompat.getColor(getActivity(), R.color.ff55b8a9));
                }
            });
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        class Holder extends RecyclerView.ViewHolder {

            TextView tv_dateTime;

            public Holder(View itemView) {
                super(itemView);
                tv_dateTime = itemView.findViewById(R.id.tv_dateTime);
            }
        }
    }


    private void getScheduleTimeList() {
        RestProxy.getInstance().getScheduleList(docId, deptId, schDate, timeInterval, "3", new BaseObserver<ScheduleListResponse>() {
            @Override
            public void onSuccess(ScheduleListResponse scheduleResponse) {
                list.clear();
                list.addAll(scheduleResponse.times);
                schTime = list.get(0).schTime.substring(0, 5);
                scheduleId = list.get(0).scheduleId;
                price = list.get(0).price;
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable e) {
                ToastUtil.onError(getActivity(), e);
            }
        });
    }
}
